<template>
  <a-layout class="layout">
    <a-layout-header>
      <div class="logo"/>
      <a-menu
          theme="dark"
          mode="horizontal"
          v-model:selectedKeys="selectedKeys"
          :style="{ lineHeight: '64px' }"
      >
        <a-menu-item key="/">
          <router-link to="/">首页</router-link>
        </a-menu-item>
        <a-menu-item key="/plan">
          <router-link to="/plan">时间计划</router-link>
        </a-menu-item>
      </a-menu>
    </a-layout-header>
    <a-layout-content style="padding: 0 50px">
      <a-breadcrumb style="margin: 16px 0"></a-breadcrumb>
      <div :style="{ background: '#fff', padding: '24px', minHeight: '280px' }">
        <a-row>
          <a-col :sapn="6">
            <a-card size="small" title="计划总用时是：" style="width: 300px">
              <template #extra><a href="#">more</a></template>
              <p>总共计时用时是：{{allTime }}</p>
              <p>card content</p>
              <p>card content</p>
            </a-card>
          </a-col>
          <a-col :span="16">
            <router-view></router-view>
          </a-col>
        </a-row>
        {{ a }}
      </div>
    </a-layout-content>
    <a-layout-footer style="text-align: center">
      {{ allTime }}
      Ant Design ©2018 Created by Ant UED
    </a-layout-footer>
  </a-layout>
</template>
<script>
// import { defineComponent, ref } from 'vue';
// export default defineComponent({
//   setup(props, ) {
//     return {
//       selectedKeys: ref(['1']),
//     };
//   },
// });
// compositionApi
import {reactive, toRefs, watch, computed, ref} from 'vue'
import {useRoute} from 'vue-router'
import {useStore} from 'vuex'

export default {
  created() {
    console.log('created')
  },
  /**
   *
   * @param props 父组件传递的属性值
   * @param context 上下文 attrs slots emit
   * @returns {{a: UnwrapNestedRefs<{a: number, selectedKeys: string[]}>["a"] extends Ref ? UnwrapNestedRefs<{a: number, selectedKeys: string[]}>["a"] : Ref<UnwrapRef<UnwrapNestedRefs<{a: number, selectedKeys: string[]}>["a"]>>, selectedKeys: UnwrapNestedRefs<{a: number, selectedKeys: string[]}>["selectedKeys"] extends Ref ? UnwrapNestedRefs<{a: number, selectedKeys: string[]}>["selectedKeys"] : Ref<UnwrapRef<UnwrapNestedRefs<{a: number, selectedKeys: string[]}>["selectedKeys"]>>}}
   */
  setup(props, context) { // 最先执行setup
    console.log('setup')
    console.log('context is:', context)
    const store = useStore();

    // 默认只执行一次
    //reactive方法返回一个响应式数据
    const state = reactive({
      // selectedKeys: ["1"],
      a: 123,
      allTime: ref(store.getters.allTime) // 单独将allTime包装成响应式数据
    });

    console.log(state);


    const route = useRoute();
    // watch 监控某个数据
    // watch(() => route.path, (newValue)=> {
    //   state.selectedKeys = [newValue]
    // }, {
    //   immediate:true // 立即执行，默认只有变化才执行。
    // });


    // 计算属性
    const selectedKeys = computed(() => {
      return [route.path]
    })

    setTimeout(() => {
      state.a = 1000;
    }, 2000)

    return {
      ...toRefs(state), // toRefs保证数据是响应式的
      selectedKeys
    }
  }
}
</script>
<style>
.site-layout-content {
  min-height: 280px;
  padding: 24px;
  background: #fff;
}

#components-layout-demo-top .logo {
  float: left;
  width: 120px;
  height: 31px;
  margin: 16px 24px 16px 0;
  background: rgba(255, 255, 255, 0.3);
}

.ant-row-rtl #components-layout-demo-top .logo {
  float: right;
  margin: 16px 0 16px 24px;
}

[data-theme='dark'] .site-layout-content {
  background: #141414;
}
</style>